package ws.utils.collections.table;

/**
 * LaTex representation of the table.
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class TableLatex extends Table {

  /**
   * Create new instance.
   */
  public TableLatex() {
  }

  /**
   * Create new instance from other table.
   *
   * @param table table to convert.
   */
  public TableLatex(ITable table) {
    header = new RowLatex(table.getHeader());
    for(Row row : table.getRowList()) {
      addRow(new RowLatex(row));
    }
  }

  /**
   * Create a string that has LaTex code that represents the data in this table.
   *
   * @return LaTex code.
   */
  @Override
  public String toString() {
    int colCount = getColumnCount();

    String latex = "\\begin{tabular}{| l |";
    for(int i = 1; i < colCount; ++i) {
      latex += " l |";
    }
    latex += "} \\hline \n";

    /* Print header. */
    latex += header.toString();

    /* Print rows. */
    for(Row row : rowList) {
//      latex += "  ";
      latex += row.toString();
    }
    latex += "\\end{tabular}\n";
    return latex;
  }

  /**
   * Convert table to LaTex representation.
   *
   * @param table table to be converted.
   *
   * @return representation of {@code table} in LaTex code.
   */
  public static String toLatex(ITable table) {
    TableLatex tlatex = new TableLatex(table);

    return tlatex.toString();
  }
}
